/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.shop.entity.Parameter;
import com.thinkgem.jeesite.modules.shop.entity.GoodsCategory;
import com.thinkgem.jeesite.modules.shop.service.ParameterService;
import com.thinkgem.jeesite.modules.shop.service.GoodsCategoryService;

/**
 * 商品参数管理Controller
 * @author xxs
 * @version 2016-03-03
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/parameter")
public class ParameterController extends BaseController {

	@Autowired
	private ParameterService parameterService;
	@Autowired
	private GoodsCategoryService goodsCategoryService;
	
	@ModelAttribute
	public Parameter get(@RequestParam(required=false) String id) {
		Parameter entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = parameterService.get(id);
		}
		if (entity == null){
			entity = new Parameter();
		}
		return entity;
	}
	
	@RequiresPermissions("shop:parameter:view")
	@RequestMapping(value = {"list", ""})
	public String list(Parameter parameter, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Parameter> page = parameterService.findPage(new Page<Parameter>(request, response), parameter); 
		model.addAttribute("page", page);
		return "modules/shop/parameterList";
	}

	@RequiresPermissions("shop:parameter:view")
	@RequestMapping(value = "form")
	public String form(Parameter parameter, Model model) {
		List<GoodsCategory> list = goodsCategoryService.findList(new GoodsCategory()); 
		model.addAttribute("list", list);
		model.addAttribute("parameter", parameter);
		return "modules/shop/parameterForm";
	}

	@RequiresPermissions("shop:parameter:edit")
	@RequestMapping(value = "save")
	public String save(Parameter parameter, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, parameter)){
			return form(parameter, model);
		}
		parameterService.save(parameter);
		addMessage(redirectAttributes, "保存商品参数成功");
		return "redirect:"+Global.getAdminPath()+"/shop/parameter/?repage";
	}
	
	@RequiresPermissions("shop:parameter:edit")
	@RequestMapping(value = "delete")
	public String delete(Parameter parameter, RedirectAttributes redirectAttributes) {
		parameterService.delete(parameter);
		addMessage(redirectAttributes, "删除商品参数成功");
		return "redirect:"+Global.getAdminPath()+"/shop/parameter/?repage";
	}

}